﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompanyBussiness
{
    public class Gallery:IGallery
    {
        private CompanyDataContext companydata = new CompanyDataContext();

        public virtual List<Album> GetAlbum(int languagesID, Boolean albumStatus, int StartRow)
        {
            var album = (from al in companydata.Albums
                         where al.LanguagesID == languagesID && al.AlbumStatus == albumStatus
                         select al).ToList();
            return album;
        }


        public virtual Album GetAlbumByID(int albumID)
        {
            var album = (from al in companydata.Albums
                         where al.AlbumID==albumID
                         select al).Single();
            return album;
        }


        public virtual List<Album> GetAlbumByName(string albumName, int languagesID, Boolean albumStatus)
        {
            var album = (from al in companydata.Albums
                         where al.LanguagesID == languagesID && al.AlbumName.Contains(albumName)==true && al.AlbumStatus == albumStatus
                         select al).ToList();
            return album;
        }

        public virtual List<Album> GetAlbumByDate(int languagesID, DateTime albumDateTime, Boolean albumStatus, int StartRow)
        {
            var album = (from al in companydata.Albums
                         where al.LanguagesID == languagesID && al.AlbumDateTime==albumDateTime && al.AlbumStatus == albumStatus
                         select al).Skip(StartRow).Take(10);
            return album.ToList();
        }

        public virtual List<Album> GetAlbumByStatus(int languagesID, Boolean albumStatus, int StartRow)
        {
            var album = (from al in companydata.Albums
                         where al.LanguagesID == languagesID && al.AlbumStatus == albumStatus
                         select al).Skip(StartRow).Take(10);
            return album.ToList();
        }


        public virtual List<Picture> GetPictureByAlbum(int albumID, Boolean pictureStatus, int StartRow)
        {
            var picture = (from pi in companydata.Pictures
                           where pi.AlbumID == albumID && pi.PictureStatus == pictureStatus
                           select pi).Skip(StartRow).Take(10); //co phan trang moi lan lay 10 picture theo album
            return picture.ToList();
        }
        public virtual Picture GetPictureByID(int pictureID)
        {
            var picture = (from pi in companydata.Pictures
                           where pi.PictureID == pictureID
                           select pi).Single();
            return picture;
        }
        public virtual List<Picture> GetPictureByName(string pictureName, int languagesID, Boolean pictureStatus, int StartRow)
        {
            var picture = (from pi in companydata.Pictures
                           join al in companydata.Albums on pi.AlbumID equals al.AlbumID
                           where pi.PictureName.Contains(pictureName) == true && pi.PictureStatus==pictureStatus && al.LanguagesID==languagesID 
                           select pi).Skip(StartRow).Take(10); //co phan trang moi lan lay 10 picture (tim kiem picture theo ten)
            return picture.ToList();
        }

        public virtual List<Picture> GetPictureByStatus(int languagesID, Boolean pictureStatus, int StartRow)
        {
            var picture = (from pi in companydata.Pictures
                           join al in companydata.Albums on pi.AlbumID equals al.AlbumID
                           where pi.PictureStatus == pictureStatus && al.LanguagesID == languagesID
                           select pi).Skip(StartRow).Take(10); //co phan trang moi lan lay 10 picture (tim kiem picture theo trang thai)
            return picture.ToList();
        }


        public virtual void CreateAlbum(string albumName, DateTime albumDateTime, string albumDescript, string albumAuthor, string albumImg, Boolean albumStatus, int languagesID)
        {
            Album album = new Album();
            album.AlbumName = albumName;
            album.AlbumDateTime = albumDateTime;
            album.AlbumDescript = albumDescript;
            album.AlbumAuthor = albumAuthor;
            album.AlbumImg = albumImg;
            album.AlbumStatus = albumStatus;
            album.LanguagesID = languagesID;

            companydata.Albums.InsertOnSubmit(album);
            companydata.SubmitChanges();
        }

        public virtual void UpdateAlbum(int albumID, string albumName, DateTime albumDateTime, string albumDescript, string albumAuthor, string albumImg, Boolean albumStatus, int languagesID)
        {
            var album = (from al in companydata.Albums
                         where al.AlbumID == albumID
                         select al).Single();

            album.AlbumName = albumName;
            album.AlbumDateTime = albumDateTime;
            album.AlbumDescript = albumDescript;
            album.AlbumAuthor = albumAuthor;
            album.AlbumImg = albumImg;
            album.AlbumStatus = albumStatus;
            album.LanguagesID = languagesID;

            companydata.SubmitChanges();
        }

        public virtual void DeleteAlbum(int albumID)
        {
            var album = (from al in companydata.Albums
                         where al.AlbumID == albumID
                         select al).Single();

            companydata.Albums.DeleteOnSubmit(album);
            companydata.SubmitChanges();
        }


        public virtual void CreatePicture(string pictureName, string pictureIntro, string pictureDes, int albumID, Boolean pictureStatus)
        {
            Picture picture = new Picture();
            picture.PictureName = pictureName;
            picture.PictureIntro = pictureIntro;
            picture.PictureDes = pictureDes;
            picture.AlbumID = albumID;
            picture.PictureStatus = pictureStatus;

            companydata.Pictures.InsertOnSubmit(picture);
            companydata.SubmitChanges();
        }

        public virtual void UpdatePicture(int pictureID, string pictureName, string pictureIntro, string pictureDes, int albumID, Boolean pictureStatus)
        {
            var picture = (from pi in companydata.Pictures
                           where pi.PictureID == pictureID
                           select pi).Single();

            picture.PictureName = pictureName;
            picture.PictureIntro = pictureIntro;
            picture.PictureDes = pictureDes;
            picture.AlbumID = albumID;
            picture.PictureStatus = pictureStatus;

            companydata.SubmitChanges();
        }

        public virtual void DeletePicture(int pictureID)
        {
            var picture = (from pi in companydata.Pictures
                           where pi.PictureID == pictureID
                           select pi).Single();

            companydata.Pictures.DeleteOnSubmit(picture);
            companydata.SubmitChanges();
        }
    }
}
